function onLoad(){
	document.title=window.arguments[0];
	buildReports();
	// Size of view
	var size=wind().get_var("reportsize");
	var tree=document.getElementById("tree");
	tree.setAttribute("rows",size);
}

function wind(){
	return window.arguments[1].TWB;
}

function clearAll(){
	var i;
	var list;
	var tree=document.getElementById("reports");
	list=tree.getElementsByTagName("treeitem");
	for(i=list.length-1; i>=0; i--){
		list[i].parentNode.removeChild(list[i]);
	}
}



function buildReports(){
	var tree=document.getElementById("reports");
	var i;
	for(i=0; i<wind()._SESSION.REPORTS.length; i++){
		var report=wind()._SESSION.REPORTS[i];
		var au=report.attackerunits;
		var du=report.defenderunits;
		
		var item=document.createElement("treeitem");
		item.setAttribute("container",true);
		item.setAttribute("open",false);
		var row=document.createElement("treerow");
		var cell=document.createElement("treecell");
		cell.setAttribute("label",report.name);
		cell.setAttribute("editable",false);
		row.appendChild(cell);
		item.appendChild(row);
		var childr=document.createElement("treechildren");
		/* First Row */
		var item_=document.createElement("treeitem");
		var row_=document.createElement("treerow");
		var cell1=document.createElement("treecell");
		cell1.setAttribute("label",report.date);
		cell1.setAttribute("editable",false);
		var cell2=document.createElement("treecell");
		cell2.setAttribute("label",report.luck);
		cell2.setAttribute("editable",false);
		var cell3=document.createElement("treecell");
		cell3.setAttribute("label",report.morale);
		cell3.setAttribute("editable",false);
		var cell4=document.createElement("treecell");
		cell4.setAttribute("label","");
		cell4.setAttribute("editable",false);
		var cell5=document.createElement("treecell");
		cell5.setAttribute("label","");
		cell5.setAttribute("editable",false);
		var cell6=document.createElement("treecell");
		cell6.setAttribute("label","");
		cell6.setAttribute("editable",false);
		var cell7=document.createElement("treecell");
		cell7.setAttribute("label","");
		cell7.setAttribute("editable",false);
		var cell8=document.createElement("treecell");
		cell8.setAttribute("label","");
		cell8.setAttribute("editable",false);
		var cell9=document.createElement("treecell");
		cell9.setAttribute("label","");
		cell9.setAttribute("editable",false);
		var cell10=document.createElement("treecell");
		cell10.setAttribute("label","");
		cell10.setAttribute("editable",false);
		var cell11=document.createElement("treecell");
		cell11.setAttribute("label","");
		cell11.setAttribute("editable",false);
		var cell12=document.createElement("treecell");
		cell12.setAttribute("label","");
		cell12.setAttribute("editable",false);
		var cell13=document.createElement("treecell");
		cell13.setAttribute("label","");
		cell13.setAttribute("editable",false);
		var cell14=document.createElement("treecell");
		cell14.setAttribute("label","");
		cell14.setAttribute("editable",false);
		var cell15=document.createElement("treecell");
		cell15.setAttribute("label","");
		cell15.setAttribute("editable",false);
		var cell16=document.createElement("treecell");	
		cell16.setAttribute("id",report.id);
		cell16.setAttribute("editable",true);
		cell16.className="del";
		row_.appendChild(cell1);
		row_.appendChild(cell2);
		row_.appendChild(cell3);
		row_.appendChild(cell4);
		row_.appendChild(cell5);
		row_.appendChild(cell6);
		row_.appendChild(cell7);
		row_.appendChild(cell8);
		row_.appendChild(cell9);
		row_.appendChild(cell10);
		row_.appendChild(cell11);
		row_.appendChild(cell12);
		row_.appendChild(cell13);
		row_.appendChild(cell14);
		row_.appendChild(cell15);
		row_.appendChild(cell16);
		item_.appendChild(row_);
		childr.appendChild(item_);
		/* Second Row */
		var item_=document.createElement("treeitem");
		var row_=document.createElement("treerow");
		var cell1=document.createElement("treecell");
		cell1.setAttribute("label","");
		cell1.setAttribute("editable",false);
		var cell2=document.createElement("treecell");
		cell2.setAttribute("label","");
		cell2.setAttribute("editable",false);
		var cell3=document.createElement("treecell");
		cell3.setAttribute("label","");
		cell3.setAttribute("editable",false);
		var cell4=document.createElement("treecell");
		cell4.setAttribute("label",au[0].spear);
		cell4.setAttribute("editable",false);
		var cell5=document.createElement("treecell");
		cell5.setAttribute("label",au[0].sword);
		cell5.setAttribute("editable",false);
		var cell6=document.createElement("treecell");
		cell6.setAttribute("label",au[0].axe);
		cell6.setAttribute("editable",false);
		var cell7=document.createElement("treecell");
		cell7.setAttribute("label",au[0].archer);
		cell7.setAttribute("editable",false);
		var cell8=document.createElement("treecell");
		cell8.setAttribute("label",au[0].spy);
		cell8.setAttribute("editable",false);
		var cell9=document.createElement("treecell");
		cell9.setAttribute("label",au[0].light);
		cell9.setAttribute("editable",false);
		var cell10=document.createElement("treecell");
		cell10.setAttribute("label",au[0].marcher);
		cell10.setAttribute("editable",false);
		var cell11=document.createElement("treecell");
		cell11.setAttribute("label",au[0].heavy);
		cell11.setAttribute("editable",false);
		var cell12=document.createElement("treecell");
		cell12.setAttribute("label",au[0].ram);
		cell12.setAttribute("editable",false);
		var cell13=document.createElement("treecell");
		cell13.setAttribute("label",au[0].catapult);
		cell13.setAttribute("editable",false);
		var cell14=document.createElement("treecell");
		cell14.setAttribute("label",au[0].knight);
		cell14.setAttribute("editable",false);
		var cell15=document.createElement("treecell");
		cell15.setAttribute("label",au[0].snob);
		cell15.setAttribute("editable",false);
		var cell16=document.createElement("treecell");
		cell16.setAttribute("editable",false);
		row_.appendChild(cell1);
		row_.appendChild(cell2);
		row_.appendChild(cell3);
		row_.appendChild(cell4);
		row_.appendChild(cell5);
		row_.appendChild(cell6);
		row_.appendChild(cell7);
		row_.appendChild(cell8);
		row_.appendChild(cell9);
		row_.appendChild(cell10);
		row_.appendChild(cell11);
		row_.appendChild(cell12);
		row_.appendChild(cell13);
		row_.appendChild(cell14);
		row_.appendChild(cell15);
		row_.appendChild(cell16);
		item_.appendChild(row_);
		childr.appendChild(item_);
		/* Third Row */
		var item_=document.createElement("treeitem");
		var row_=document.createElement("treerow");
		var cell1=document.createElement("treecell");
		cell1.setAttribute("label","");
		cell1.setAttribute("editable",false);
		var cell2=document.createElement("treecell");
		cell2.setAttribute("label","");
		cell2.setAttribute("editable",false);
		var cell3=document.createElement("treecell");
		cell3.setAttribute("label","");
		cell3.setAttribute("editable",false);
		var cell4=document.createElement("treecell");
		cell4.setAttribute("label",au[1].spear);
		cell4.setAttribute("editable",false);
		var cell5=document.createElement("treecell");
		cell5.setAttribute("label",au[1].sword);
		cell5.setAttribute("editable",false);
		var cell6=document.createElement("treecell");
		cell6.setAttribute("label",au[1].axe);
		cell6.setAttribute("editable",false);
		var cell7=document.createElement("treecell");
		cell7.setAttribute("label",au[1].archer);
		cell7.setAttribute("editable",false);
		var cell8=document.createElement("treecell");
		cell8.setAttribute("label",au[1].spy);
		cell8.setAttribute("editable",false);
		var cell9=document.createElement("treecell");
		cell9.setAttribute("label",au[1].light);
		cell9.setAttribute("editable",false);
		var cell10=document.createElement("treecell");
		cell10.setAttribute("label",au[1].marcher);
		cell10.setAttribute("editable",false);
		var cell11=document.createElement("treecell");
		cell11.setAttribute("label",au[1].heavy);
		cell11.setAttribute("editable",false);
		var cell12=document.createElement("treecell");
		cell12.setAttribute("label",au[1].ram);
		cell12.setAttribute("editable",false);
		var cell13=document.createElement("treecell");
		cell13.setAttribute("label",au[1].catapult);
		cell13.setAttribute("editable",false);
		var cell14=document.createElement("treecell");
		cell14.setAttribute("label",au[1].knight);
		cell14.setAttribute("editable",false);
		var cell15=document.createElement("treecell");
		cell15.setAttribute("label",au[1].snob);
		cell15.setAttribute("editable",false);
		var cell16=document.createElement("treecell");
		cell16.setAttribute("editable",false);
		row_.appendChild(cell1);
		row_.appendChild(cell2);
		row_.appendChild(cell3);
		row_.appendChild(cell4);
		row_.appendChild(cell5);
		row_.appendChild(cell6);
		row_.appendChild(cell7);
		row_.appendChild(cell8);
		row_.appendChild(cell9);
		row_.appendChild(cell10);
		row_.appendChild(cell11);
		row_.appendChild(cell12);
		row_.appendChild(cell13);
		row_.appendChild(cell14);
		row_.appendChild(cell15);
		row_.appendChild(cell16);
		item_.appendChild(row_);
		childr.appendChild(item_);
		var sep=document.createElement("treeseparator");
		childr.appendChild(sep);
		/* Forth Row */
		var item_=document.createElement("treeitem");
		var row_=document.createElement("treerow");
		var cell1=document.createElement("treecell");
		cell1.setAttribute("label","");
		cell1.setAttribute("editable",false);
		var cell2=document.createElement("treecell");
		cell2.setAttribute("label","");
		cell2.setAttribute("editable",false);
		var cell3=document.createElement("treecell");
		cell3.setAttribute("label","");
		cell3.setAttribute("editable",false);
		var cell4=document.createElement("treecell");
		cell4.setAttribute("label",du[0].spear);
		cell4.setAttribute("editable",false);
		var cell5=document.createElement("treecell");
		cell5.setAttribute("label",du[0].sword);
		cell5.setAttribute("editable",false);
		var cell6=document.createElement("treecell");
		cell6.setAttribute("label",du[0].axe);
		cell6.setAttribute("editable",false);
		var cell7=document.createElement("treecell");
		cell7.setAttribute("label",du[0].archer);
		cell7.setAttribute("editable",false);
		var cell8=document.createElement("treecell");
		cell8.setAttribute("label",du[0].spy);
		cell8.setAttribute("editable",false);
		var cell9=document.createElement("treecell");
		cell9.setAttribute("label",du[0].light);
		cell9.setAttribute("editable",false);
		var cell10=document.createElement("treecell");
		cell10.setAttribute("label",du[0].marcher);
		cell10.setAttribute("editable",false);
		var cell11=document.createElement("treecell");
		cell11.setAttribute("label",du[0].heavy);
		cell11.setAttribute("editable",false);
		var cell12=document.createElement("treecell");
		cell12.setAttribute("label",du[0].ram);
		cell12.setAttribute("editable",false);
		var cell13=document.createElement("treecell");
		cell13.setAttribute("label",du[0].catapult);
		cell13.setAttribute("editable",false);
		var cell14=document.createElement("treecell");
		cell14.setAttribute("label",du[0].knight);
		cell14.setAttribute("editable",false);
		var cell15=document.createElement("treecell");
		cell15.setAttribute("label",du[0].snob);
		cell15.setAttribute("editable",false);
		var cell16=document.createElement("treecell");
		cell16.setAttribute("editable",false);
		row_.appendChild(cell1);
		row_.appendChild(cell2);
		row_.appendChild(cell3);
		row_.appendChild(cell4);
		row_.appendChild(cell5);
		row_.appendChild(cell6);
		row_.appendChild(cell7);
		row_.appendChild(cell8);
		row_.appendChild(cell9);
		row_.appendChild(cell10);
		row_.appendChild(cell11);
		row_.appendChild(cell12);
		row_.appendChild(cell13);
		row_.appendChild(cell14);
		row_.appendChild(cell15);
		row_.appendChild(cell16);
		item_.appendChild(row_);
		childr.appendChild(item_);
		/* Fifth Row */
		var item_=document.createElement("treeitem");
		var row_=document.createElement("treerow");
		var cell1=document.createElement("treecell");
		cell1.setAttribute("label","");
		cell1.setAttribute("editable",false);
		var cell2=document.createElement("treecell");
		cell2.setAttribute("label","");
		cell2.setAttribute("editable",false);
		var cell3=document.createElement("treecell");
		cell3.setAttribute("label","");
		cell3.setAttribute("editable",false);
		var cell4=document.createElement("treecell");
		cell4.setAttribute("label",du[1].spear);
		cell4.setAttribute("editable",false);
		var cell5=document.createElement("treecell");
		cell5.setAttribute("label",du[1].sword);
		cell5.setAttribute("editable",false);
		var cell6=document.createElement("treecell");
		cell6.setAttribute("label",du[1].axe);
		cell6.setAttribute("editable",false);
		var cell7=document.createElement("treecell");
		cell7.setAttribute("label",du[1].archer);
		cell7.setAttribute("editable",false);
		var cell8=document.createElement("treecell");
		cell8.setAttribute("label",du[1].spy);
		cell8.setAttribute("editable",false);
		var cell9=document.createElement("treecell");
		cell9.setAttribute("label",du[1].light);
		cell9.setAttribute("editable",false);
		var cell10=document.createElement("treecell");
		cell10.setAttribute("label",du[1].marcher);
		cell10.setAttribute("editable",false);
		var cell11=document.createElement("treecell");
		cell11.setAttribute("label",du[1].heavy);
		cell11.setAttribute("editable",false);
		var cell12=document.createElement("treecell");
		cell12.setAttribute("label",du[1].ram);
		cell12.setAttribute("editable",false);
		var cell13=document.createElement("treecell");
		cell13.setAttribute("label",du[1].catapult);
		cell13.setAttribute("editable",false);
		var cell14=document.createElement("treecell");
		cell14.setAttribute("label",du[1].knight);
		cell14.setAttribute("editable",false);
		var cell15=document.createElement("treecell");
		cell15.setAttribute("label",du[1].snob);
		cell15.setAttribute("editable",false);
		var cell16=document.createElement("treecell");
		cell16.setAttribute("editable",false);
		row_.appendChild(cell1);
		row_.appendChild(cell2);
		row_.appendChild(cell3);
		row_.appendChild(cell4);
		row_.appendChild(cell5);
		row_.appendChild(cell6);
		row_.appendChild(cell7);
		row_.appendChild(cell8);
		row_.appendChild(cell9);
		row_.appendChild(cell10);
		row_.appendChild(cell11);
		row_.appendChild(cell12);
		row_.appendChild(cell13);
		row_.appendChild(cell14);
		row_.appendChild(cell15);
		row_.appendChild(cell16);
		item_.appendChild(row_);
		childr.appendChild(item_);
		
		item.appendChild(childr);
		tree.appendChild(item);
	}
}

function onOK(){
	// Confirm Delete
	var i, j;
	var todelete=[];
	var flag=false;
	var cells=document.getElementsByClassName("del");
	for(i=0; i<cells.length; i++){
		if(cells[i].getAttribute("value")=="true"){
			flag=true;
			break;
		}
	}
	if(flag==false || (flag==true && confirm(wind().lang("confdelreport")))){
		// Fetch delete
		for(i=0; i<cells.length; i++){
			if(cells[i].getAttribute("value")=="true"){
				// Schedule for delete 
				todelete.push(cells[i].getAttribute("id"));
			}
		}
		// Delete
		for(i=0; i<todelete.length; i++){
			for(j=0; j<wind()._SESSION.REPORTS.length; j++){
				if(wind()._SESSION.REPORTS[j].id==todelete[i]){
					wind()._SESSION.REPORTS.splice(j,1);
					break;
				}
			}
		}
		return true;
	}
	else{
		return false;
	}
}